﻿Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Serialization
Imports Newtonsoft.Json.Linq

Public Enum animationList
    'None
    SlideUp
    SlideDown
    SlideLeft
    SlideRight
End Enum

Public Enum languageList
    'None
    Indonesian
    English

End Enum

'Public Enum Top_Background
'   Pic1
'  Pic2
' Pic3
'Pic4
'Pic5
'End Enum

Public Class UserControl1
    'Dim backs As backgroundgambar
    Dim animasi As animationList
    Dim bahasa As languageList
    'Dim bg As Top_Background

    Public jawab

    Dim alamat As String = "http://technubi.com/sand-android/pebeka.php"
    Dim x As Integer
    Dim y As Integer
    Dim interval As Integer = 0
    Dim berubah As Integer = 10
    Dim posisi As Integer
    Dim posisiLeft As Integer
    Dim posisiRight As Integer
    Dim o As JObject
    Dim side1 As Integer = 0
    Dim side2 As Integer = 0
    Dim counter As Integer = 0
    Public Shared ayatemas As New Dictionary(Of String, String)
    Dim ayatemas2 As New Dictionary(Of String, String)
    Dim arisi(100) As String
    Dim arayat(100) As String
    '-------------------------warna font bunyi
    Public Property CommandmentFontColor() As System.Drawing.Color
        Get
            Return Label1.ForeColor
        End Get

        Set(ByVal Value As System.Drawing.Color)
            Label1.ForeColor = Value
        End Set
    End Property

    '-------------------------warna font ayat
    Public Property VerseFontColor() As System.Drawing.Color
        Get
            Return Label2.ForeColor
        End Get

        Set(ByVal Value As System.Drawing.Color)
            Label2.ForeColor = Value
        End Set
    End Property

    '------------------------- jenis font ayat
    Public Property VerseFont() As System.Drawing.Font
        Get
            Return Label2.Font
        End Get

        Set(ByVal Value As System.Drawing.Font)
            Label2.Font = Value
        End Set
    End Property

    '------------------------- jenis font ayat
    Public Property CommandmentFont() As System.Drawing.Font
        Get
            Return Label1.Font
        End Get

        Set(ByVal Value As System.Drawing.Font)
            Label1.Font = Value
        End Set
    End Property

    '------------------------- background atas
    'Public Property Top_BackGround() As Top_Background
    '   Get
    '      Return Me.bg
    ' End Get

    '    Set(ByVal Value As Top_Background)
    '       Me.bg = Value
    '  End Set
    'End Property

    Public Property TopBackground As Image
        Get
            Return PictureBox1.Image
        End Get
        Set(value As Image)
            PictureBox1.Image = value
        End Set
    End Property

    Public Property VerseBackColor As System.Drawing.Color
        Get
            Return Label2.BackColor
        End Get
        Set(value As System.Drawing.Color)
            Label2.BackColor = value
        End Set
    End Property

    Public Property CommandmentBackColor As System.Drawing.Color
        Get
            Return Label1.BackColor
        End Get
        Set(value As System.Drawing.Color)
            Label1.BackColor = value
        End Set
    End Property
    '------------------------- background warna form keseluruhan
    Public Property BottomBackgroundColor As System.Drawing.Color
        Get
            Return Me.BackColor
        End Get

        Set(ByVal Value As System.Drawing.Color)
            Me.BackColor = Value
        End Set
    End Property

    '------------------------- background gambar form keseluruhan
    Public Property BottomBackgroundPicture As Image
        Get
            Return Me.BackgroundImage

        End Get

        Set(ByVal Value As Image)
            Me.BackgroundImage = Value
        End Set
    End Property


    Public Property ChangeEvery As Integer
        Get
            Return berubah
        End Get
        Set(value As Integer)
            berubah = value
        End Set
    End Property


    Public Sub formLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'tampil()
        Cari.Hide()
        Label1.Location = New Point(13, 80)
        Label2.Location = New Point(144, 169)
        posisi = Label1.Top
        posisiLeft = Label1.Left
        posisiRight = Label1.Right
        Showed()
        'PictureBox1.Image = 
        'animasi = animationList.None
    End Sub

    Public imgList As New List(Of Bitmap)
    Public Property Prop As List(Of Bitmap)
        Get
            Return imgList.ToList()
        End Get
        Set(value As List(Of Bitmap))
            'Me.BackgroundImage
        End Set
    End Property

    Public Property Animation As animationList
        Get
            Return Me.animasi
        End Get
        Set(value As animationList)
            If (value = animationList.SlideUp) Then
                Label1.Location = New Point(13, 80)
                Label2.Location = New Point(144, 169)
            Else
                Label1.Location = New Point(13, 80)
                Label2.Location = New Point(144, 169)
            End If
            Me.animasi = value
        End Set
    End Property


    Public Property Language As LanguageList
        Get
            Return Me.bahasa
        End Get
        Set(value As languageList)

            If (value = languageList.English) Then
                alamat = "http://technubi.com/sand-android/pebeka2.php"

                Showed()
            ElseIf (value = languageList.Indonesian) Then
                alamat = "http://technubi.com/sand-android/pebeka.php"

                Showed()
            End If
            Me.bahasa = value


        End Set
    End Property


    'Public Property TopBackground As Top_Background
    '   Get
    '      If TopBackground = Top_Background.Pic1 Then
    '         PictureBox1.Image = My.Resources.Chrysanthemum
    '    ElseIf TopBackground = Top_Background.Pic2 Then
    '       PictureBox1.Image = My.Resources.Jellyfish
    '  End If
    ' Return Me.bg
    'End Get
    'Set(value As Top_Background)
    '    Me.bg = value
    'End Set
    'End Property

    

    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

    End Sub
    

    
    Private Sub Showed()
        ayatemas.Clear()
        
        Dim batas As Integer = 0
        Dim searching As String = Cari.Text
        Dim request As WebRequest = WebRequest.Create(alamat)

        request.Method = "POST"
        Dim postData As String
        Dim kode As String
        Dim hasilAyat As String

        If Cari.TextLength = 0 Then
            postData = "kode=random"

            'Label2.Text = postData
            'Label3.Text = postData
        Else
            kode = "non"
            postData = "kode=" & kode & "&&cari=" & searching
            'Label2.Text = postData
            'Label3.Text = postData
        End If
        'MessageBox.Show(postData)
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As WebResponse = request.GetResponse()
        Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream, Encoding.UTF8)
        Dim responseFromServer As String = reader.ReadToEnd()


        Dim counter As Integer = 0
        If responseFromServer = "0" Then
            Label1.Text = "No Data Found"
        Else

            hasilAyat = responseFromServer
            'Label1.Text = responseFromServer
            'MessageBox.Show(responseFromServer)
            Dim jResults As JArray = JArray.Parse(responseFromServer)
            Dim results As List(Of JToken) = jResults.Children().ToList()
            Dim ayat1 As String = ""
            Dim isi1 As String = ""
            Dim counter2 As Integer = 0
            For Each item As JObject In results
                Dim demo As String = ""
                For Each prop As JProperty In item.Properties()
                    If (counter2 = 0) Then
                        ayat1 = prop.Value.ToString
                        counter2 = counter2 + 1
                    Else
                        isi1 = prop.Value.ToString
                        counter2 = 0
                    End If
                Next
                arisi(batas) = isi1
                arayat(batas) = ayat1
                batas = batas + 1
            Next
            
        End If
        reader.Close()
        dataStream.Close()
        response.Close()
        side1 = 0
        side2 = 0
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Animation = animationList.SlideUp Then
            slideUp()
        ElseIf Animation = animationList.SlideDown Then
            slideDown()
        ElseIf Animation = animationList.SlideLeft Then
            slideLeft()
            If Label1.Right = posisiRight Then
                Timer1.Enabled = False
            End If
        ElseIf Animation = animationList.SlideRight Then
            slideRight()
            If Label1.Right = posisiRight Then
                Timer1.Enabled = False
            End If
        End If
        If (Animation = animationList.SlideUp Or Animation = animationList.SlideDown) And Label1.Top = posisi Then
            Timer1.Enabled = False
            'ElseIf (Animation = animation.SlideLeft Or Animation = animation.SlideRight) And Label1.Right = posisiRight Then
            '   Timer1.Enabled = False
        End If
    End Sub


    Private Sub slideUp()
        If side1 = arayat.Length() Then
            side1 = 0
            side2 = 0
        End If
        If Label1.Location.Y <= 0 Then
            Label1.Location = New Point(Label1.Location.X, 250)
            Label2.Visible = False
            side1 = side1 + 1
            Label1.Text = arayat(side1)
        Else
            Label1.Top = Label1.Top - 5
        End If
        If Label2.Location.Y <= 0 Then
            Label2.Location = New Point(Label2.Location.X, 250)
            Label2.Visible = True
            side2 = side2 + 1
            Label2.Text = arisi(side2)
        Else
            Label2.Top = Label2.Top - 5
        End If
        counter = counter + 1
        If (counter = 30) Then
            counter = 0
        End If
    End Sub

    Private Sub slideDown()
        If side1 = arayat.Length() Then
            side1 = 0
            side2 = 0
        End If
        If Label1.Location.Y > 250 Then
            Label1.Visible = True
            Label1.Location = New Point(Label1.Location.X, 0)
            side1 = side1 + 1
            Label1.Text = arayat(side1)
        Else
            Label1.Top = Label1.Top + 5
        End If
        If Label2.Location.Y > 250 Then
            'Label3.Text = "ada di 0"
            Label2.Location = New Point(Label2.Location.X, 0)
            Label1.Visible = False
            side2 = side2 + 1
            Label2.Text = arisi(side2)
            'showing()
        Else
            'Label3.Text = "berkurag"
            Label2.Top = Label2.Top + 5
        End If
        counter = counter + 1
        If (counter = ayatemas.Count) Then
            counter = 0
        End If
    End Sub

    Private Sub slideLeft()
        If Label1.Left <= 0 Then
            Label1.Location = New Point(253, Label1.Location.Y)
            Label2.Visible = False
            side1 = side1 + 1
            Label1.Text = arayat(side1)
        Else
            Label1.Left = Label1.Left - 5
        End If
        If Label2.Left <= 0 Then
            'Label3.Text = "ada di 0"
            Label2.Location = New Point(253, Label2.Location.Y)
            Label2.Visible = True
            side2 = side2 + 1
            Label2.Text = arisi(side2)

        Else
            'Label3.Text = "berkurag"
            Label2.Left = Label2.Left - 5
        End If
        counter = counter + 1
        If (counter = ayatemas.Count) Then
            counter = 0
        End If
    End Sub

    Private Sub slideRight()
        If Label2.Left >= 250 Then
            Label1.Visible = False
            Label2.Visible = False
            Label1.Location = New Point(-252, Label1.Location.Y)
            side1 = side1 + 1
            Label1.Text = arayat(side1)
            Label1.Visible = True
            Label2.Location = New Point(-120, Label2.Location.Y)
            side2 = side2 + 1
            Label2.Text = arisi(side2)
            Label2.Visible = True
        Else
            Label1.Left = Label1.Left + 5
            Label2.Left = Label2.Left + 5
        End If
        
        counter = counter + 1
        If (counter = ayatemas.Count) Then
            counter = 0
        End If
    End Sub

    Private Sub Search_Click(sender As Object, e As EventArgs) Handles Search.Click
        If Cari.Visible = False Then
            Cari.Show()
        Else
            Cari.Hide()
            Showed()
            Cari.Clear()
        End If
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        interval = interval + 1
        'Label2.Text = interval.ToString
        If interval Mod berubah = 0 Then
            'Label2.Text = "masuk if1"
            'Label1.Location = New Point(13, 80)
            'Label2.Location = New Point(144, 169)
            Timer1.Enabled = True
        End If
    End Sub

    Private Sub Cari_TextChanged(sender As Object, e As EventArgs) Handles Cari.TextChanged

    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub
End Class